package com.apkicon.test;

import net.dongliu.apk.parser.ApkFile;
import com.apkicon.parser.ResourcesArscParser;
import java.util.Map;

/**
 * 专门分析test.apk中的颜色资源
 * 查找可能被遗漏的蓝色颜色定义
 */
public class ColorResourceAnalyzer {
    
    public static void main(String[] args) {
        try {
            System.out.println("Analyzing Color Resources in test.apk");
            System.out.println("=====================================");
            
            try (ApkFile apkFile = new ApkFile("test.apk")) {
                // 解析resources.arsc文件
                byte[] arscData = apkFile.getFileData("resources.arsc");
                if (arscData != null) {
                    System.out.println("Found resources.arsc: " + arscData.length + " bytes");
                    
                    // 解析颜色资源
                    Map<Integer, String> colorResources = ResourcesArscParser.parseResourcesArsc(arscData);
                    
                    System.out.println("\nColor Resources Found:");
                    System.out.println("======================");
                    
                    if (colorResources.isEmpty()) {
                        System.out.println("No color resources found in resources.arsc");
                    } else {
                        for (Map.Entry<Integer, String> entry : colorResources.entrySet()) {
                            int resourceId = entry.getKey();
                            String colorHex = entry.getValue();
                            
                            System.out.println("Resource ID: 0x" + Integer.toHexString(resourceId) + " = " + colorHex);
                            
                            // 检查是否是蓝色
                            if (isBlueColor(colorHex)) {
                                System.out.println("  *** THIS IS BLUE! ***");
                            }
                        }
                    }
                    
                    // 尝试手动查找蓝色颜色值
                    System.out.println("\nManual Blue Color Search:");
                    System.out.println("=========================");
                    findBlueColorsInArsc(arscData);
                    
                } else {
                    System.out.println("resources.arsc not found");
                }
            }
            
        } catch (Exception e) {
            System.err.println("Analysis failed: " + e.getMessage());
            e.printStackTrace();
        }
    }
    
    private static boolean isBlueColor(String colorHex) {
        if (colorHex == null || !colorHex.startsWith("#")) {
            return false;
        }
        
        try {
            int color = Integer.parseInt(colorHex.substring(1), 16);
            int red = (color >> 16) & 0xFF;
            int green = (color >> 8) & 0xFF;
            int blue = color & 0xFF;
            
            // 检查是否是蓝色（蓝色分量明显大于红色和绿色）
            return blue > red + 50 && blue > green + 50 && blue > 100;
        } catch (NumberFormatException e) {
            return false;
        }
    }
    
    private static void findBlueColorsInArsc(byte[] arscData) {
        // 查找常见的蓝色值
        int[] blueColors = {
            0xFF0000FF,  // 纯蓝色
            0xFF0066CC,  // 深蓝色
            0xFF2196F3,  // Material Blue
            0xFF1976D2,  // Material Blue 700
            0xFF1E88E5,  // Material Blue 600
            0xFF42A5F5,  // Material Blue 400
            0xFF64B5F6,  // Material Blue 300
            0xFF90CAF9,  // Material Blue 200
            0xFFBBDEFB,  // Material Blue 100
            0xFFE3F2FD,  // Material Blue 50
            0xFF007BFF,  // Bootstrap Blue
            0xFF0056B3,  // Bootstrap Blue Dark
            0xFF4285F4,  // Google Blue
            0xFF1A73E8,  // Google Blue Dark
        };
        
        for (int blueColor : blueColors) {
            byte[] colorBytes = {
                (byte) (blueColor & 0xFF),
                (byte) ((blueColor >> 8) & 0xFF),
                (byte) ((blueColor >> 16) & 0xFF),
                (byte) ((blueColor >> 24) & 0xFF)
            };
            
            int position = findBytesInArray(arscData, colorBytes);
            if (position != -1) {
                System.out.println("Found blue color " + String.format("#%08X", blueColor) + " at position " + position);
            }
        }
    }
    
    private static int findBytesInArray(byte[] array, byte[] pattern) {
        for (int i = 0; i <= array.length - pattern.length; i++) {
            boolean found = true;
            for (int j = 0; j < pattern.length; j++) {
                if (array[i + j] != pattern[j]) {
                    found = false;
                    break;
                }
            }
            if (found) {
                return i;
            }
        }
        return -1;
    }
}
